#include "loginwidget.h"

LoginWidget::LoginWidget(QWidget *parent) :QWidget (parent)
{
    init_Widget();
    init_Control();
    init_Connect();

}

void LoginWidget::init_Control()
{
    //登录窗口控件
    //按钮
    this->loginBtn=new QPushButton();
    this->loginBtn->setText(QString::fromUtf8("登录"));
    this->loginBtn->setMaximumSize(100,70);

    this->registerBtn=new QPushButton();
    this->registerBtn->setText(QString::fromUtf8("注册"));
    this->registerBtn->setMaximumSize(100,70);

    this->outBtn=new QPushButton();
    this->outBtn->setText(QString::fromUtf8("退出"));
    this->outBtn->setMaximumSize(100,70);
    //标签
    this->usernameLab=new QLabel();
    this->usernameLab->setMaximumSize(100,70);
    this->usernameLab->setText(QString::fromUtf8("账号:"));

    this->passwordLab=new QLabel();
    this->passwordLab->setMaximumSize(50,70);
    this->passwordLab->setText(QString::fromUtf8("密码:"));

    //文本框
    this->usernameEdit=new QLineEdit();
    this->usernameEdit->setMaximumSize(200,70);
    this->passwordEdit=new QLineEdit();
    this->passwordEdit->setMaximumSize(200,70);

    //布局管理器
    this->layEdit=new QHBoxLayout();
    this->layEdit->addWidget(loginBtn);
    this->layEdit->addWidget(registerBtn);
    this->layEdit->addWidget(outBtn);

    this->layname=new QHBoxLayout();
    //this->layname->setMargin(200);
    this->layname->addStretch(1);
    this->layname->addWidget(usernameLab);
    this->layname->addWidget(usernameEdit);
    this->layname->addStretch(1);

    this->laypswd=new QHBoxLayout();
    //this->laypswd->setMargin(200);
    this->laypswd->addStretch(1);
    this->laypswd->addWidget(passwordLab);
    this->laypswd->addWidget(passwordEdit);
    this->laypswd->addStretch(1);

    this->laycombined=new QVBoxLayout();
    this->laycombined->addLayout(layname);
    this->laycombined->addLayout(laypswd);
    this->laycombined->addLayout(layEdit);


    this->setLayout(this->laycombined);
    //this->setLayout(this->layEdit);
    //this->setLayout(this->layname);
    //this->setLayout(this->laypswd);
}

void LoginWidget::init_Widget()
{
     //窗口设置
    this->setWindowTitle(QString::fromUtf8("登录窗口"));
    this->resize(800,600);
    //mainWidget=new MainWidget();
    //this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint);
    managerWidget= new ManagerWidget();
    userWidget=new UserWidget();

}

void LoginWidget::init_Connect()
{
    connect(this->loginBtn,SIGNAL(clicked()),this,SLOT(on_loginBtn_cliked()));
}

int LoginWidget::loginCheck()
{
    //登录检测
    QString name=this->usernameEdit->text();
    QString password=this->passwordEdit->text();
    QString sql=QString("SELECT *FROM Users WHERE user_name='%1';").arg(name);
    //打开数据库并查询结果并存入user
    this->model=control->getInstance()->Creatmodel("user");
    //判断账号是否在数据库中
    //this->user=nullptr;
    //qDebug()<<"查询用户";
    this->user=model->getUserByName((char*)sql.toStdString().c_str());
    //qDebug()<<user->getUser_identity();
    //判断密码
    if(user)
    {
        if(user->getUserPassword()==password)
        {    //判断用户是用户还是管理员还是普通用户
            if(user->getUser_identity()==0)
            {
                return 0;
            }
            else if (user->getUser_identity()==1)
            {

                return 1;
            }

        }
        else
        {
            //qDebug()<<"1";

            QMessageBox::critical(this, "错误", "账号或密码错误");
            this->usernameEdit->clear();
            this->passwordEdit->clear();
            return 0;
        }
    }
    else
    {
        //qDebug()<<"0";
        QMessageBox::critical(this, "错误", "账号或密码错误");
        this->usernameEdit->clear();
        this->passwordEdit->clear();
        return 0;
    }
}

void LoginWidget::on_regBtn_cliked()
{

}

void LoginWidget::on_loginBtn_cliked()
{
   int check;
   qDebug()<<"进入判断";
   check=loginCheck();
    if(check==0)
    {

        this->hide();
        this->userWidget->show();
    }
    else if(check==1)
    {

        this->hide();
        this->managerWidget->show();
    }

}
